package com.fankui.serviceorder.service.impl;

import com.fankui.commonutils.R;
import com.fankui.serviceorder.client.CourseClient;
import com.fankui.serviceorder.client.UcenterClient;
import com.fankui.serviceorder.entity.Order;
import com.fankui.serviceorder.mapper.OrderMapper;
import com.fankui.serviceorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fankui.serviceorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 范奎
 * @since 2023-12-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private CourseClient courseClient;
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String saveOrder(String courseId, String memberId) {
        R courseR = courseClient.getCourseDetails(courseId);
        Map<String, Object> course = (Map<String, Object>) courseR.getData().get("courseDetailVo");
        R memberR = ucenterClient.getMemberVo(memberId);
        Map<String, Object> member = (Map<String, Object>) memberR.getData().get("memberVo");
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle((String) course.get("title"));
        order.setCourseCover((String) course.get("cover"));
        order.setTeacherName((String) course.get("teacherName"));
        order.setTotalFee(BigDecimal.valueOf((Double) course.get("price")));
        order.setMemberId(memberId);
        order.setMobile((String) member.get("mobile"));
        order.setNickname((String) member.get("nickname"));
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
